﻿using Microsoft.Owin.StaticFiles;
using Newtonsoft.Json.Serialization;
using Owin;
using Sustainalytics.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sustainalytics.AdminInterface.Service
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureFileServer(app);
        }

        private void ConfigureFileServer(IAppBuilder app)
        {
            // use a file server to serve all static content (js, css, content, html, ...) and also configure default files (eg: index.html to be the default entry point)
            // usage: https://github.com/omacarena/novaburst.ModularTypeScript

            var aggFileSystemConfig = HostConfig.GetAggregateFileSystemConfig();

            // create file system that will locate the files
            var fileSystem = AggregateFileSystem.FromConfig(aggFileSystemConfig);

            // setup default documents
            app.UseDefaultFiles(new DefaultFilesOptions
            {
                FileSystem = fileSystem,
                DefaultFileNames = new List<string>
                {
                    "views/index.html"
                }
            });


            // start file server to share website static content
            // wrapper around: StaticFiles + DefaultFiles + DirectoryBrowser
            var fileServerOptions = new FileServerOptions
            {
                EnableDirectoryBrowsing = false,
                FileSystem = fileSystem,
            };

            fileServerOptions.StaticFileOptions.ContentTypeProvider = new FileServerContentTypeProvider();

            app.UseFileServer(fileServerOptions);
        }
    }
}
